package com.maila.xiaoaoschool.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.QuickContactBadge;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.entity.ShopClasses;
import com.maila.xiaoaoschool.util.Constants;
import com.maila.xiaoaoschool.widget.SectionedBaseAdapter;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/26.
 */

public class ShopDetailsAdapter extends SectionedBaseAdapter {
    Context context;
    List<ShopClasses.DataBean> foodbeanList;
    CartNumLinstenr callback;
    public ShopDetailsAdapter(Context context,CartNumLinstenr callback) {
        this.context = context;
        this.callback=callback;
        foodbeanList = new ArrayList<>();
    }

    public List<ShopClasses.DataBean> getFoodbeanList() {
        return foodbeanList;
    }

    public void setFoodbeanList(List<ShopClasses.DataBean> foodbeanList) {
        this.foodbeanList = foodbeanList;
        notifyDataSetChanged();
    }

    public void addFoodbeanList(List<ShopClasses.DataBean> foodbeanList) {
        this.foodbeanList.addAll(foodbeanList);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        this.foodbeanList.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public ShopClasses.DataBean.GoodBean getItem(int section, int position) {
        return foodbeanList.get(section).getGood().get(position);
    }

    @Override
    public long getItemId(int section, int position) {
        return 0;
    }

    @Override
    public int getSectionCount() {
        return foodbeanList.size();
    }

    @Override
    public int getCountForSection(int section) {
        return foodbeanList.get(section).getGood().size();
    }

    @Override
    public View getItemView(final int section, final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shop_detail, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvGoodsName.setText(foodbeanList.get(section).getGood().get(position).getGoodsname());
        Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO + foodbeanList.get(section).getGood().get(position).getImg())).into(holder.ivGoods);
        holder.tvXiaoliang.setText("已售：" + foodbeanList.get(section).getGood().get(position).getSellcount());
        if(foodbeanList.get(section).getGood().get(position).getIs_cx()!=1&&foodbeanList.get(section).getGood().get(position).getIs_new()!=1){
            holder.tvTip.setVisibility(View.GONE);
            holder.tvFee.setText("￥" + foodbeanList.get(section).getGood().get(position).getCost());
            holder.tvYuan.setVisibility(View.GONE);
        }else {
            holder.tvTip.setVisibility(View.VISIBLE);
            holder.tvFee.setText("￥" + foodbeanList.get(section).getGood().get(position).getCxcost());
            holder.tvYuan.setText("￥" + foodbeanList.get(section).getGood().get(position).getCost());
            holder.tvYuan.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );

            if(foodbeanList.get(section).getGood().get(position).getIs_cx()==1){
                holder.tvTip.setText(foodbeanList.get(section).getGood().get(position).getCxzhe()+"折  限"+foodbeanList.get(section).getGood().get(position).getCxnum()+"份");
            }else if(foodbeanList.get(section).getGood().get(position).getIs_new()==1){
                holder.tvTip.setText("今日特价");
            }
        }


        if(foodbeanList.get(section).getGood().get(position).getBuynum()>0){
            holder.btnReduce.setVisibility(View.VISIBLE);
            holder.tvNum.setVisibility(View.VISIBLE);
            holder.tvNum.setText(foodbeanList.get(section).getGood().get(position).getBuynum()+"");
        }else {
            holder.btnReduce.setVisibility(View.GONE);
            holder.tvNum.setVisibility(View.GONE);
        }
        holder.btnReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                foodbeanList.get(section).getGood().get(position).setBuynum(foodbeanList.get(section).getGood().get(position).getBuynum()-1);
                callback.updatenum(foodbeanList.get(section).getGood().get(position),0,position);
                notifyDataSetChanged();
            }
        });
        holder.btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                foodbeanList.get(section).getGood().get(position).setBuynum(foodbeanList.get(section).getGood().get(position).getBuynum()+1);
                callback.updatenum(foodbeanList.get(section).getGood().get(position),1,position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        ViewHolder1 holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shop_detail_header, parent, false);
            holder = new ViewHolder1(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder1) convertView.getTag();
        }
        holder.tvCategoryName.setText(foodbeanList.get(section).getTypename());
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.iv_shop)
        ImageView ivGoods;
        @BindView(R.id.tv_shop_name)
        TextView tvGoodsName;
        @BindView(R.id.tv_xiaoliang)
        TextView tvXiaoliang;
        @BindView(R.id.tv_fee)
        TextView tvFee;
        @BindView(R.id.btn_reduce)
        ImageView btnReduce;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.btn_plus)
        ImageView btnPlus;
        @BindView(R.id.tv_tip)
        TextView tvTip;
        @BindView(R.id.tv_yuan)
        TextView tvYuan;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 {
        @BindView(R.id.tv_category_name)
        TextView tvCategoryName;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public interface CartNumLinstenr{
        void updatenum(ShopClasses.DataBean.GoodBean date,int plus,int position);
    }
}
